package com.etc.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


import com.etc.dao.AnnouncementDao;
import com.etc.dao.BaseDao;
import com.etc.entity.Announcement;
import com.etc.entity.PageCount;





public class AnnouncementDaoImpl extends BaseDao implements AnnouncementDao{

	@Override
	public List<Announcement> listAnnouncement() {
		
		String sql = "select * from forums_announcement";
		return this.listResult(Announcement.class,sql,null);
	}

	@Override
	public int addAnnouncement(Announcement announcement) {
		String sql = "INSERT INTO forums_announcement(username,title,content)VALUE(?,?,?)";
		Object[] args = {announcement.getUsername(),announcement.getTitle(),announcement.getContent()};
		return this.myExecuteUpdate(sql, args);
	}





	@Override
	public int deleteAnnouncement(long id) {
		String sql ="DELETE FROM forums_announcement WHERE id=?";
		Object[] args = { id };
		return this.myExecuteUpdate(sql, args);
	}



	@Override
	public Announcement loadannouncement(long id) {
		String sql ="SELECT  * FROM forums_announcement WHERE id=?";
		Object[] args = {id};
		return this.loadResult(Announcement.class, sql, args);
	}
	public Long countAll() {
		String sql ="SELECT  count(1) as totalCount FROM forums_announcement ";
		Object[] args = {};
		return this.loadResult(PageCount.class, sql, args).getTotalCount();
	}

	@Override
	public int modifyAnnouncement(Announcement announcement) {
		String sql ="";
		if(announcement.getUsername()!=null&&announcement.getUsername()!="") {
	    sql ="UPDATE forums_announcement SET username=?,title=?,content=? WHERE id=?";
        Object[] args = { announcement.getUsername(),announcement.getTitle(), announcement.getContent(),announcement.getId()};
		return this.myExecuteUpdate(sql, args);
	}else
	{
		sql ="UPDATE forums_announcement SET username=?,title=?,content=? WHERE id=?";
        Object[] args = { announcement.getUsername(),announcement.getTitle(), announcement.getContent(),announcement.getId()};
		return this.myExecuteUpdate(sql, args);
		
	}
	
}
}